import {reqregister} from '../../request/api'
import { Button, Form, Input,Toast } from 'antd-mobile'

//初始状态
const initState={
       user:{
        phone:'',
        nickname:'',
        password:''
       }
}

//TYPES
const TYPES={
        CHANGE_REGUSER:'CHANGE_REGUSER'
}

//reducer
const reducer =(state=initState,action)=>{
        switch(action.type){
                // {type:'CHANGE_REGUSER',key:'11',value:'111'}
                case TYPES.CHANGE_REGUSER:
                        return{
                                ...state,
                                user:{
                                        ...state.user,
                                        [action.key]:action.value

                                }
                        }
                default:
                        return state
        }
}

//creators 做逻辑 方法  请求
export const actions={
        changeuser:(key,value)=>({type:TYPES.CHANGE_REGUSER,key,value}),
        //点击注册发送请求
        doRegister:(push)=>{
                return (dispacth,getState)=>{
                    let user =getUser(getState())
                    reqregister(user).then(res=>{
                            if(res.data.code==200){
                                    Toast.show({
                                            content:res.data.msg
                                    })
                                    //跳转登录
                                    push('/login')
                            }
                    })
                }
        }
}

// 导出数据给组件
export const getUser=(state)=>state.register.user
// 导出reducer
export default reducer